package codeoffloading;

import java.net.Socket;
import java.util.ArrayList;

import netcom.messages.ExecutionRequestMessage;

public class CodeOffloadingManager {

	public static CodeOffloadingManager instance = null;
	private ArrayList<ExecutionThread> eT;
	
	
	public static CodeOffloadingManager getInstance(){
		if(instance == null) instance = new CodeOffloadingManager();
		return instance;
	}
	
	
	public CodeOffloadingManager(){
		eT = new  ArrayList<ExecutionThread>();
	}
	
	
	public void executionRequest(Socket sock, ExecutionRequestMessage m){
		ExecutionThread t = new ExecutionThread(sock, m);
		new Thread(t).run();
		synchronized(this){
			
			//clean the list
			ArrayList<ExecutionThread> toRemove = new ArrayList<ExecutionThread>();
			for(ExecutionThread t1: eT){
				if(t1.hasFinished()){
					toRemove.add(t1);
				}
			}
			
			for(ExecutionThread t2: toRemove){
				eT.remove(t2);
			}
			
			//add thread on the list
			eT.add(t);
		}
		
	}
	
	
}

